﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win.UltraWinGrid;

namespace CKNetFrameWork
{
    public partial class Frm_Seller : DockContent
    {
        private LSY.BLL.LSY_SellerTab m_bllLSY_SellerTab = new LSY.BLL.LSY_SellerTab();
        private LSY.BLL.LSY_AgentTab m_bllLSY_AgentTab = new LSY.BLL.LSY_AgentTab();
        private int m_nDelIndex = -1;
        private bool m_bInsertFlag = false;
        public Frm_Seller()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void ShowAllRecordInDataGridView(string strWhere)
        {
            ultraGrid1.DataSource = null;
            DataSet ds = m_bllLSY_SellerTab.GetList(strWhere);

            DataSet dsTemp = m_bllLSY_AgentTab.GetList("");

            this.ultraDropDownDep.DataSource = dsTemp.Tables[0];
            this.ultraDropDownDep.DisplayMember = "代理商名称";
            this.ultraDropDownDep.ValueMember = "LSY_AgentTabID";
            this.ultraDropDownDep.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownDep.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownDep.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownDep.DisplayLayout.Bands[0].Columns["代理商名称"].Hidden = false;

            //if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                this.ultraGrid1.DisplayLayout.Bands[0].Columns["代理商"].ValueList = this.ultraDropDownDep;
            }
        }
        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView("");
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }

        }
        private bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                LSY.Model.LSY_SellerTab modelLSY_SellerTab = new LSY.Model.LSY_SellerTab();

                modelLSY_SellerTab.LSY_AgentTabID = Convert.ToInt32(CurrentUGR.Cells["代理商"].Value.ToString(), 10);
                modelLSY_SellerTab.LSY_SellerName = CurrentUGR.Cells["零售商名称"].Value.ToString();
                modelLSY_SellerTab.LSY_SellerPerson = CurrentUGR.Cells["零售商联系人"].Value.ToString();
                modelLSY_SellerTab.LSY_SellerAddress = CurrentUGR.Cells["零售商地址"].Value.ToString();
                modelLSY_SellerTab.LSY_SellerPhone = CurrentUGR.Cells["零售商电话"].Value.ToString();
 

                int nId = m_bllLSY_SellerTab.Add(modelLSY_SellerTab);

                ShowAllRecordInDataGridView("");

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private void InitNewAddRowValue(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["代理商"].Value = 1;
            CurrentUGR.Cells["零售商名称"].Value = "";
            CurrentUGR.Cells["零售商地址"].Value = "";
            CurrentUGR.Cells["零售商联系人"].Value = "";
            CurrentUGR.Cells["零售商电话"].Value = "";

            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[0].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetail(nIndex);

            }

        }

        private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;
            InitNewAddRowValue(e.Row.Index);
        }

        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {

                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                LSY.Model.LSY_SellerTab modelLSY_SellerTab = m_bllLSY_SellerTab.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

   


                modelLSY_SellerTab.LSY_SellerName = CurrentUGR.Cells["零售商名称"].Value.ToString();
                modelLSY_SellerTab.LSY_SellerPerson = CurrentUGR.Cells["零售商联系人"].Value.ToString();
                modelLSY_SellerTab.LSY_SellerAddress = CurrentUGR.Cells["零售商地址"].Value.ToString();
                modelLSY_SellerTab.LSY_SellerPhone = CurrentUGR.Cells["零售商电话"].Value.ToString();

                string strBGH = CurrentUGR.Cells["代理商"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelLSY_SellerTab.LSY_AgentTabID = Convert.ToInt32(CurrentUGR.Cells["代理商"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllLSY_AgentTab.GetList("LSY_AgentName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelLSY_SellerTab.LSY_AgentTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["LSY_AgentTabID"].ToString(), 10);
                    }
                }


                m_bllLSY_SellerTab.Update(modelLSY_SellerTab);


                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;
                    if (!AddGoodsDetail(e.Row.Index))
                        e.Cancel = true;



                }
                else//修改
                {

                    ModifyGoodsDetail(e.Row.Index);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!m_bInsertFlag)
            {
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells[0].Value.ToString(), 10);
                LSY.Model.LSY_SellerTab modelLSY_SellerTabOld = m_bllLSY_SellerTab.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

                if (modelLSY_SellerTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    try
                    {

                        m_bllLSY_SellerTab.Delete(nSelectFinRecvMoneyDetailTab_NewID);



                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void 增加一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!m_bInsertFlag)
            {
                UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
            }
        }

        private void 删除一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }

                if (ultraGrid1.ActiveRow != null)
                {

                    ultraGrid1.ActiveRow.Delete(false);
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

    }
}
